/* history.vala
 *
 * Copyright (C) 2010  Richard Talbot-Watkins
 * Copyright (C) 2010  Dave Jeffery
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * Authors:
 *  Richard Talbot-Watkins <richtw1@gmail.com>
 * 	Dave Jeffery <david.richard.jeffery@gmail.com>
 */

/**
 * History.
 *
 * Stores the undo/redo history.
 *
 * @author Richard Talbot-Watkins <richtw1@gmail.com>
 * @author Dave Jeffery <david.richard.jeffery@gmail.com>
 * @version 2010.1227
 * @since 0.1
 */
private class MonsterDesigner.History : GLib.Object {

	/**
	 * The undo buffer.
	 */
    private CircularStack<Clone> undo_buffer = new CircularStack<Clone>(64);

	/**
	 * The redo buffer.
	 */
    private CircularStack<Clone> redo_buffer = new CircularStack<Clone>(64);

	/**
	 * The object to keep a history for.
	 */
	private SpriteData sprite_data;

	/**
	 * Constructor for the History class.
	 * 
	 * @param sprite_data The object we're keeping track of.
	 */
	public History (SpriteData sprite_data) {
		this.sprite_data = sprite_data;
	}

	/**
	 * Remember the current state in the undo buffer.
	 * 
	 * Call this before doing some undoable operation.
	 */
	public void add () {
		redo_buffer.clear ();
		undo_buffer.push(sprite_data.clone ());
	}

	/**
	 * Called when undo activated.
	 */
	public void undo () {
		if (undo_buffer.count > 0) {
			redo_buffer.push(sprite_data.clone ());
			sprite_data.set_from_clone (undo_buffer.pop ());
		}
	}

	/**
	 * Called when redo activated.
	 */
	public void redo () {
		if (redo_buffer.count > 0) {
			undo_buffer.push(sprite_data.clone ());
			sprite_data.set_from_clone (redo_buffer.pop ());
		}
	}
}
